import {
  userFollowItem,
  userUnfollow,
  isItemFollow,
  itemFansSum,
} from "@/server/api/itemApi";
import { ref } from "vue";
import { useUseStore } from "@/stores/user";

let followId = useUseStore().userId; //
export let isfollow = ref(); // 用户是否关注该店铺
export let shopFansNum = ref(); // 网店粉丝数

// 网店关注网店接口
export const userFollowItemFun = async (itemId) => {
  console.log("关注", itemId);
  let res = await userFollowItem({ followId, itemId });
  //   console.log("网店关注网店接口", res.data.data);

  // 调用接口判断用户是否关注该店铺,为了使关注按钮切换,并且更新网店粉丝数
  isfollow.value = true;
  shopFansNum.value += 1;
};

// 网店取关网店接口
export const userUnfollowFun = async (itemId) => {
  console.log("itemId", itemId);
  let res = await userUnfollow({ followId, itemId });
  // console.log("网店取关网店接口", res.data.data);

  // 调用接口判断用户是否关注该店铺,为了使关注按钮切换,并且更新网店粉丝数
  isfollow.value = false;
  shopFansNum.value -= 1;
};

// 查询用户是否已经关注该网店
export const isItemFollowFun = async (shopId,userId) => {
  let res = await isItemFollow(shopId,userId );
  isfollow.value = res.data.data;
  // console.log("用户是否已经关注该网店", isfollow.value);
};

// 查询网店粉丝总数接口
export const itemFansSumFun = async (itemId) => {
  let res = await itemFansSum(itemId);

  shopFansNum.value = res.data.data;
  // console.log("查询网店粉丝总数接口", shopFansNum.value);
};
